<?php
namespace App\Services;

use App\Models\Appoint;
use App\Models\TimeConfig;
use Illuminate\Support\Facades\Redis;

class AppointService {

//    public static function getLeftNum() {
//        $appoint_max_num = ConfigService::getValue(ConfigService::APPOINT_MAX_NUM);
//        $today_appoint_num = Appoint::where('date', date('Y-m-d'))->count();
//        return $appoint_max_num - $today_appoint_num;
//    }

    public static function order($user_id) {
        $info = Appoint::where('user_id', $user_id)
            ->first();
        return $info;
    }

    private static function getCacheKey($date) {
        return 'order_' . $date;
    }

    public static function getLeftNum($date = null, $time = 1) {
        if (env('APPOINT_IN_REDIS', true)) {
            $key = self::getCacheKey($date);
            \Cache::rememberForever($key, function () use($key, $date, $time){
                $list = ConfigService::getAll();
                foreach ($list as $item) {
                    $child_cache_key = $key . "_" . $item->key;
                    $len = Redis::llen($child_cache_key);
                    if ($len == 0) {
                        $today_appoint_num = Appoint::where('date', $date)->where('time', $time)->count();
                        if ($today_appoint_num == 0) {
                            for($i = $item->start_at;$i <= $item->end_at; $i++) {
                                Redis::lpush($child_cache_key, $i);
                            }
                        }
                    }
                }
                return 'success';
            });
            $child_cache_key = $key . "_" . $time;
            return Redis::llen($child_cache_key);
        } else {
            $timeConfig = TimeConfig::where('key', $time)->first();
            $total = $timeConfig->end_at - $timeConfig->start_at + 1;
            $appoint_count = Appoint::where('date', $date)->where('time', $time)->count();
            $left = $total - $appoint_count;
            return $left >= 0 ? $left : 0;
        }
    }

    public static function getOrderNo($date, $time) {
//        return null;
        if ($date == '2020-04-04') {
            return null;
        }
        if (env('APPOINT_IN_REDIS', true)) {
            $key = self::getCacheKey($date);
            $child_cache_key = $key . "_" . $time;
            return Redis::rpop($child_cache_key);
        } else {
            $timeConfig = TimeConfig::where('key', $time)->first();
            $arr_no = Appoint::where('date', $date)->where('time', $time)->get()->pluck('order_no')->all();
            for ($i = $timeConfig->start_at; $i <= $timeConfig->end_at; $i++) {
                $order_no =date('md', strtotime($date)) . str_pad($i,4,"0",STR_PAD_LEFT);
                if (!in_array($order_no, $arr_no)) {
                    return substr($order_no, -4, 4);
                }
            }
            return null;
        }
    }

    public static function insertOrderNoToCache($order_no, $date, $time) {
        if (env('APPOINT_IN_REDIS', true)) {
            $key = self::getCacheKey($date);
            $child_cache_key = $key . "_" . $time;
            return Redis::rpush($child_cache_key, $order_no);
        }
    }



}